package com.sun.kamun.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 拦截器：验证用户是否登录
 */
public class UserLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 检查用户是否已登录
        HttpSession session = request.getSession();
        if (session.getAttribute("user") != null||session.getAttribute("admin") != null) {
            // 用户已登录，允许请求继续执行
            return true;
        } else {
            // 用户未登录，重定向到登录页面
            response.sendRedirect("/kamun/user/login");
//            response.sendRedirect("/kamun/user/login?status=expired");
            return false;
        }
    }
}